package com.cytech.cloudclinic.http.interceptor;

import android.text.TextUtils;

import com.yechaoa.yutils.SpUtil;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

import static com.cytech.cloudclinic.app.Constant.USER_TOKEN;


/**
 * http请求头拦截器
 */
public class HeaderInterceptor implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {
        String token = SpUtil.getString(USER_TOKEN);
        //如果token为空的话直接返回原请求，不为空则加上token
        if(TextUtils.isEmpty(token)){
            Request originalRequest = chain.request();
            return chain.proceed(originalRequest);
        }else{
            Request.Builder builder = chain.request().newBuilder();
            builder.addHeader("Authorization",token);
            return chain.proceed(builder.build());
        }
    }
}
